package controller;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Record;

import model.Answer;
import model.Collect;
import model.Question;
import model.ReadLog;
import model.User;
import service.HomeService;

public class HomeController extends Controller {
	public void question() {
		Question q = getModel(Question.class);
		if(q.save()) {
			renderJson("{\"status\":\"success\"}" );
		}else {
			renderJson("{\"status\":\"no\"}" );
		}
	}
	
	public void getList(String uid) {
		HomeService hs = new HomeService();
		renderJson(hs.getList(get("uid")));
	}
	
	public void getInfo(String uid) {
		HomeService hs = new HomeService();
		hs.changePd(Integer.valueOf(get("aid")),Integer.valueOf(get("uid")), 1);
		renderJson(hs.getInfo(get("aid"),get("uid")));
	}
	
	public void getQuestion(String uid) {
		HomeService hs = new HomeService();
		renderJson(hs.getQuestion(get("uid")));
	}
	public void getMyQuestion(String uid) {
		HomeService hs = new HomeService();
		renderJson(hs.getMyQuestion(get("uid")));
	}
	public void getMyAnswer(String uid) {
		HomeService hs = new HomeService();
		renderJson(hs.getMyAnswer(get("uid")));
	}
	
	public void getQuestionByID() {
		HomeService hs = new HomeService();
		renderJson(hs.getQuestionByID(get("qid")));
	}
	public void changePWD() {
		User u = getModel(User.class);
		if(u.update()) {
			renderJson("{\"status\":\"success\"}" );
		}else {
			renderJson("{\"status\":\"no\"}" );
		}
	}
	public void addAnswer() {
		Answer answer = getModel(Answer.class);
		if(answer.save()) {
			renderJson("{\"status\":\"success\"}" );
		}else {
			renderJson("{\"status\":\"no\"}" );
		}
	}
	public void up() {
		HomeService hs = new HomeService();
		Answer answer = getModel(Answer.class);
		answer.setUp(answer.getUp()+1);
		answer.setHot(answer.getHot()+1);
		if(answer.update()&&hs.changePd(answer.getAid(), answer.getUid(), 1)) {
			renderJson("{\"status\":\"success\"}" );
		}else {
			renderJson("{\"status\":\"no\"}" );
		}
	}
	public void down() {
		Answer answer = getModel(Answer.class);
		answer.setDown(answer.getDown()+1);
		answer.setHot(answer.getHot()+1);
		if(answer.update()) {
			renderJson("{\"status\":\"success\"}" );
		}else {
			renderJson("{\"status\":\"no\"}" );
		}
	}
	public void collect () {
		HomeService hs = new HomeService();
		Collect c = getModel(Collect.class);
		if(c.save()&&hs.changePd(c.getAid(), c.getUid(), 1)) {
			renderJson("{\"status\":\"success\"}" );
		}else {
			renderJson("{\"status\":\"no\"}" );
		}
	}
	public void deletCollect () {
		Collect c = getModel(Collect.class);
		if(c.delete()) {
			renderJson("{\"status\":\"success\"}" );
		}else {
			renderJson("{\"status\":\"no\"}" );
		}
	}
	public void getCollect() {
		HomeService hs = new HomeService();
		renderJson(hs.getCollect(get("uid")));
	}
	
	public void getSelect() {
		HomeService hs = new HomeService();
		renderJson(hs.select(get("key")));
	}
	
	public void log() {
		HomeService hs = new HomeService();
		ReadLog r = getModel(ReadLog.class);
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        r.setTime(df.format(new Date()));// new Date()为获取当前系统时间
        List<Record> l = hs.isDouble(r.getUid()+"", r.getAid()+"");
        if(l.size()>0) {
        	r.setPd(l.get(0).getFloat("pd")+1);
        	r.setId(l.get(0).getInt("id"));
        	l=null;
        	if(r.update()) {
    			renderJson("{\"status\":\"success\"}" );
    		}else {
    			renderJson("{\"status\":\"no\"}" );
    		}
        }else {
        	r.setPd((float) 1);
        	if(r.save()) {
    			renderJson("{\"status\":\"success\"}" );
    		}else {
    			renderJson("{\"status\":\"no\"}" );
    		}
		}
	}

}
